package com.cro.prison.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cro.prison.common.ErrorBehaviorMethod;
import com.cro.prison.dao.DisciplineDao;
import com.cro.prison.entity.constant.DISCIPLINE_PRISONER;
import com.cro.prison.entity.pojo.Discipline;
import com.cro.prison.entity.vo.DailyBehaviorVo;
import com.cro.prison.entity.vo.DisciplineVo;
import com.cro.prison.service.DisciplineService;
import com.cro.prison.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.*;

@Service
public class DisciplineServiceImpl extends ServiceImpl<DisciplineDao, Discipline>  implements DisciplineService {

    @Override
    public List<DisciplineVo> getDiscipline(Long personId, Date beginDate, Date endDate) {

        //拿到当前日期区间对应的所有异常
        List<Discipline> dailyBehaviorList = this.baseMapper.getDiscipline(personId, beginDate, endDate);
        //按照异常类型进行分组
        Map<Integer,List<Discipline>> map = new HashMap();
        for (Discipline dailyBehavior : dailyBehaviorList) {
            List<Discipline> behaviorList = map.getOrDefault(dailyBehavior.getAbnormalBehaviorType(), new ArrayList<>());
            behaviorList.add(dailyBehavior);
            map.put(dailyBehavior.getAbnormalBehaviorType(),behaviorList);
        }

        List<DisciplineVo> result = new ArrayList<>();
        for (DISCIPLINE_PRISONER value : DISCIPLINE_PRISONER.values()) {
            //对于每一种类型,从map中寻找
            DisciplineVo vo = new DisciplineVo();
            List<Discipline> currentBehaviors = map.get(value.getCode());
            if(currentBehaviors!= null){
                Map<String,List<Object>> periodBehaviorMap = new HashMap<>();
                for (Discipline dailyBehavior : currentBehaviors) {
                    List<Object> list = periodBehaviorMap.getOrDefault(DateUtils.customFormat(dailyBehavior.getCreateTime()), new ArrayList<>());
                    list.add(dailyBehavior);
                    periodBehaviorMap.put(DateUtils.customFormat(dailyBehavior.getCreateTime()),list);
                }
                vo.setType(value.getName());
                vo.setTimes(ErrorBehaviorMethod.getTimes(beginDate,endDate,periodBehaviorMap));
                vo.setPersonId(-1l);
            }else{
                //当前类型没有
                vo.setType(value.getName());
                vo.setPersonId(-1l);
                //放入一个全是0的数组
                vo.setTimes(ErrorBehaviorMethod.getTimes(beginDate,endDate,new HashMap<>()));
            }
            result.add(vo);
        }

        return result;
    }

    @Override
    public List<DisciplineVo> getAllDiscipline(Date begin, Date end) {
        //拿到当前日期区间对应的所有异常
        List<Discipline> dailyBehaviorList = this.baseMapper.getAllDiscipline(begin, end);
        //按照异常类型进行分组
        Map<Integer,List<Discipline>> map = new HashMap();
        for (Discipline dailyBehavior : dailyBehaviorList) {
            List<Discipline> behaviorList = map.getOrDefault(dailyBehavior.getAbnormalBehaviorType(), new ArrayList<>());
            behaviorList.add(dailyBehavior);
            map.put(dailyBehavior.getAbnormalBehaviorType(),behaviorList);
        }
        List<DisciplineVo> result = new ArrayList<>();
        for (DISCIPLINE_PRISONER value : DISCIPLINE_PRISONER.values()) {
            //对于每一种类型,从map中寻找
            DisciplineVo vo = new DisciplineVo();
            List<Discipline> currentBehaviors = map.get(value.getCode());
            if(currentBehaviors!= null){
                Map<String,List<Object>> periodBehaviorMap = new HashMap<>();
                for (Discipline dailyBehavior : currentBehaviors) {
                    List<Object> list = periodBehaviorMap.getOrDefault(DateUtils.customFormat(dailyBehavior.getCreateTime()), new ArrayList<>());
                    list.add(dailyBehavior);
                    periodBehaviorMap.put(DateUtils.customFormat(dailyBehavior.getCreateTime()),list);
                }
                vo.setType(value.getName());
                vo.setTimes(ErrorBehaviorMethod.getTimes(begin,end,periodBehaviorMap));
                vo.setPersonId(-1l);
            }else{
                //当前类型没有
                vo.setType(value.getName());
                vo.setPersonId(-1l);
                //放入一个全是0的数组
                vo.setTimes(ErrorBehaviorMethod.getTimes(begin,end,new HashMap<>()));
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    public List<Discipline> getDisciplineByPersonIdAndDate(Long personId, Date date, Integer type) {
        Date dateAfter = new Date(DateUtils.addDay(date,1).getTime());
        QueryWrapper query = ErrorBehaviorMethod.getQueryByPersonIdAndDate(personId, date, dateAfter, type);
        return this.baseMapper.selectList(query);
    }




}
